package com.baikeyang.service.resource.impl;


import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.Resource;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.mapper.ResourceMapper;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baikeyang.service.resource.IResourceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2018/7/17.
 */
@Service(value = "resourceService")
public class ResourceServiceImpl extends SuperServiceImpl<ResourceMapper, Resource> implements IResourceService {

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public IPage<Resource> getResourcePage(IPage<Resource> pageParam) {
        QueryWrapper<Resource> wr = new QueryWrapper<Resource>();
        return resourceMapper.selectPage(pageParam, wr);
    }

    @Override
    public List<Resource> getResourceParentAll() {
        return resourceMapper.selectResourceParentAll();
    }

    @Override
    public ResponseResult saveResource(Resource resource) {
        resource.setCreateTime(new Date());
        Integer flag = resourceMapper.insert(resource);
        if (flag > 0) {
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult update(Resource resource) {
        Integer flag = resourceMapper.updateById(resource);
        if (flag > 0) {
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult delete(Integer id) {
        int flag = resourceMapper.deleteById(id);
        if(flag > 0){
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult delete(String ids) {
        if(StringUtils.isNotEmpty(ids)){
            List<String> idList = Arrays.asList(ids.split(","));
            QueryWrapper<Resource> wrapper = new QueryWrapper<Resource>();
            wrapper.in("id", idList);
            int flag = resourceMapper.delete(wrapper);
            if(flag > 0 && flag == idList.size()){
                return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public List<Resource> getAllResources() {
        QueryWrapper<Resource> wrapper = new QueryWrapper<Resource>();
        return resourceMapper.selectList(wrapper);
    }
}
